package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.repository.StrategyCommentRepository;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * 攻略评论的业务层(服务层)实现类方法
 */
@Service
//@Transactional mongodb的事务必须是复制集的方式
public class StrategyCommentServiceImpl implements IStrategyCommentService{
    @Autowired
    private StrategyCommentRepository repository;

    @Autowired
    private MongoTemplate template;

    @Autowired
    private IStrategyCommentService strategyCommentService;


    //添加攻略评论
    @Override
    public void save(StrategyComment strategyComment) {
        strategyComment.setId(null);//把id设置为null
        strategyComment.setCreateTime(new Date());
        repository.save(strategyComment);//存入strategyComment对象

    }

    //编辑
    @Override
    public void update(StrategyComment strategyComment) {
        repository.save(strategyComment);//编辑和保存是一样的,更新之后则要保存strategyComment对象

    }

    //删除
    @Override
    public void delete(String id) {
        repository.deleteById(id);

    }
    //查询一条数据
    @Override
    public StrategyComment get(String id) {
        //orEls,查询id,存在就返回,不承诺在就返回null
        return repository.findById(id).orElse(null);

    }
    //查询多个数据
    @Override
    public List<StrategyComment> list() {
        //查询所有
        return repository.findAll();
    }

    //分页查询
    @Override
    public Page<StrategyComment> query(StrategyCommentQuery qo) {

        //分页查询
        //查询对象
        Query query = new Query();
        //
        if (StringUtils.hasLength(qo.getStrategyId())){
            query.addCriteria(Criteria.where("strategyId").is(qo.getStrategyId()));
        }
        //随着时间倒序排序
        PageRequest pageable = PageRequest.of(qo.getCurrentPage() - 1,
                qo.getPageSize(), Sort.Direction.DESC, "createTime");
        return DBHelper.query(template,StrategyComment.class,query,pageable);
    }

    //判断是更新还是添加操作
    @Override
    public void saveOrUpdate(StrategyComment strategyComment) {

        if (StringUtils.hasLength(strategyComment.getId())){
            //update
            //特殊案例,页面更新的数据,刚好跟集合中字段个数一致
            //所以这种等价于全量的更新
            this.update(strategyComment);
        }else {
            //save
            this.save(strategyComment);
        }

    }

    /**
     *  用户评论点赞接口
     */

    @Override
    public void commentThumb(String cid, String uid) {
        //查询评论用户对象,然后从对象中拿到用户的id
        StrategyComment comment = strategyCommentService.get(cid);
        List<String> uidList = comment.getThumbuplist();
        //判断当前用户是否在UIDList集合中
        if (!uidList.contains(uid)){
            //如果为空,表示客户之前没有点赞过,点赞数+1,将用户的id存储到UID的集合中
            comment.setThumbupnum(comment.getThumbupnum()+1);
            uidList.add(uid);//然后把当前传进来的用户id添加到UID的list集合中
        }else {
            //如果不为空,表示客户之前点赞过,点赞数-1,将用户的id从UID的集合中移除
            comment.setThumbupnum(comment.getThumbupnum()-1);
            uidList.remove(uid);
        }
        //更新数据
//        strategyCommentService.update(comment);
        this.update(comment);

    }


}
